package br.com.exemplo.vendas.apresentacao.service ;

import java.util.Arrays ;
import java.util.List ;

import br.com.exemplo.vendas.apresentacao.delegate.BusinessDelegate ;
import br.com.exemplo.vendas.negocio.model.vo.ClienteVO;
import br.com.exemplo.vendas.negocio.model.vo.ComprasVO;
import br.com.exemplo.vendas.negocio.model.vo.ItemVO;
import br.com.exemplo.vendas.negocio.model.vo.ProdutoVO;
import br.com.exemplo.vendas.negocio.model.vo.UsuarioVO ;
import br.com.exemplo.vendas.util.dto.ServiceDTO ;
import br.com.exemplo.vendas.util.exception.LayerException ;

public class Service
{
	public String enviaMensagem( String mensagem ) throws LayerException
	{
		ServiceDTO requestDTO = new ServiceDTO( ) ;
		ServiceDTO responseDTO = new ServiceDTO( ) ;
		requestDTO.set( "teste", mensagem ) ;
		responseDTO = BusinessDelegate.getInstance( ).inserirQueue( requestDTO ) ;
		return ( String ) responseDTO.get( "ticket" ) ;
	}
	
	public Boolean inserirUsuario( UsuarioVO vo ) throws LayerException
	{
		ServiceDTO requestDTO = new ServiceDTO( ) ;
		ServiceDTO responseDTO = new ServiceDTO( ) ;

		requestDTO.set( "usuarioVO", vo ) ;
		responseDTO = BusinessDelegate.getInstance( ).inserirUsuario( requestDTO ) ;
		Boolean sucesso = ( Boolean ) responseDTO.get( "resposta" ) ;

		return sucesso ;
	}

	public List<UsuarioVO> listarUsuarios( ) throws LayerException
	{
		ServiceDTO requestDTO = new ServiceDTO( ) ;
		ServiceDTO responseDTO = new ServiceDTO( ) ;

		responseDTO = BusinessDelegate.getInstance( ).selectionarTodosUsuarios( requestDTO ) ;
		UsuarioVO[ ] usuarios = ( UsuarioVO[ ] ) responseDTO.get( "listaUsuario" ) ;
		List<UsuarioVO> lista = Arrays.asList( usuarios ) ;
		return lista ;
	}

	public Boolean alterarUsuario( UsuarioVO vo ) throws LayerException
	{
		ServiceDTO requestDTO = new ServiceDTO( ) ;
		ServiceDTO responseDTO = new ServiceDTO( ) ;

		requestDTO.set( "usuarioVO", vo ) ;
		responseDTO = BusinessDelegate.getInstance( ).alterarUsuario( requestDTO ) ;
		Boolean sucesso = ( Boolean ) responseDTO.get( "resposta" ) ;

		return sucesso ;
	}

	public Boolean excluirUsuario( UsuarioVO vo ) throws LayerException
	{
		ServiceDTO requestDTO = new ServiceDTO( ) ;
		ServiceDTO responseDTO = new ServiceDTO( ) ;

		requestDTO.set( "usuarioVO", vo ) ;
		responseDTO = BusinessDelegate.getInstance( ).excluirUsuario( requestDTO ) ;
		Boolean sucesso = ( Boolean ) responseDTO.get( "resposta" ) ;

		return sucesso ;
	}
	
	public ClienteVO validarUsuario( UsuarioVO vo ) throws LayerException
	{
		ServiceDTO requestDTO = new ServiceDTO( ) ;
		ServiceDTO responseDTO = new ServiceDTO( ) ;

		requestDTO.set( "usuarioVO", vo ) ;
		responseDTO = BusinessDelegate.getInstance( ).validarUsuario( requestDTO ) ;
		ClienteVO cliente = (ClienteVO) responseDTO.get( "cliente" ) ;
		
		return cliente ;
	}
	
	public List<ComprasVO> listarComprasServicoA( ) throws LayerException
	{
		ServiceDTO requestDTO = new ServiceDTO( ) ;
		ServiceDTO responseDTO = new ServiceDTO( ) ;

		responseDTO = BusinessDelegate.getInstance( ).listarCompras( requestDTO ) ;
		ComprasVO[ ] compras = ( ComprasVO[ ] ) responseDTO.get( "listaCompra" ) ;
		List<ComprasVO> lista = Arrays.asList( compras ) ;
		return lista ;
	}
	
	public List<ProdutoVO> listarProdutosServicoB( ) throws LayerException
	{
		ServiceDTO requestDTO = new ServiceDTO( ) ;
		ServiceDTO responseDTO = new ServiceDTO( ) ;

		responseDTO = BusinessDelegate.getInstance( ).listarProdutos( requestDTO ) ;
		ProdutoVO[ ] produtos = ( ProdutoVO[ ] ) responseDTO.get( "listaProduto" ) ;
		List<ProdutoVO> lista = Arrays.asList( produtos ) ;
		return lista ;
	}
	
	public List<ClienteVO> listarClientesServicoC( ) throws LayerException
	{
		ServiceDTO requestDTO = new ServiceDTO( ) ;
		ServiceDTO responseDTO = new ServiceDTO( ) ;

		responseDTO = BusinessDelegate.getInstance( ).listarClientes( requestDTO ) ;
		ClienteVO[ ] clientes = ( ClienteVO[ ] ) responseDTO.get( "listaCliente" ) ;
		List<ClienteVO> lista = Arrays.asList( clientes ) ;
		return lista ;
	}
	
	public List<ItemVO> listarPedidosServicoD( ) throws LayerException
	{
		ServiceDTO requestDTO = new ServiceDTO( ) ;
		ServiceDTO responseDTO = new ServiceDTO( ) ;

		responseDTO = BusinessDelegate.getInstance( ).listarItens( requestDTO ) ;
		ItemVO[ ] pedidos = ( ItemVO[ ] ) responseDTO.get( "listaPedido" ) ;
		List<ItemVO> lista = Arrays.asList( pedidos ) ;
		return lista ;
	}
}